﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Benefit.Models.Sys;
namespace Benefit.Models.Month
{
    /// <summary>
    /// 盈利或亏损所占的Tick
    /// </summary>
    public class GroupMonthWinOrLoseTick
    {
        public int Id { get; set; }

        public int GroupId { get; set; }

        public virtual Groups Group { get; set; }

        public int Year { get; set; }

        public int Month { get; set; }

        public bool Win { get; set; }

        public int Tick { get; set; }

        public int Count { get; set; }

        //public void Delete(int tradeHistoryId, DB.DBManager db)
        //{
        //    TradeHistory th = new TradeHistory();
        //    th = th.GetModel(tradeHistoryId, db);
        //    var query = db.GroupMonthWinOrLoseTick.Where(a => a.Month == th.MPdate).Where(a => a.Year == th.YPdate);
        //    foreach (Data.Month.GroupMonthWinOrLoseTick tick in query)
        //    {
        //        db.GroupMonthWinOrLoseTick.Remove(tick);
        //    }
        //    db.SaveChanges();
        //}

        //public void InitGroupMonthWinOrLoseTick(int tradeHistoryId, bool win, DB.DBManager db)
        //{
        //    TradeHistory th = new TradeHistory();
        //    th = th.GetModel(tradeHistoryId, db);

        //    var groups = db.Groups.ToList();

        //    foreach (Sys.Groups g in groups)
        //    {
        //        List<View.OperatorMonthTick> allticks = new List<View.OperatorMonthTick>();
        //        List<Operator> operators = new Sys.DayGroupOperators().GetGroupOperators(g.Id, Convert.ToDateTime(th.YPdate + "-" + th.MPdate + "-" + th.DPdate), db);
        //        foreach (Operator op in operators)
        //        {
        //            View.OperatorMonthTick tick = new View.OperatorMonthTick();
        //            List<View.OperatorMonthTick> ticks = tick.GetOperatorMonthWinOrLostTicks(op.Id, th.MPdate, th.YPdate, win, db);
        //            foreach (View.OperatorMonthTick t in ticks)
        //            {
        //                var query = allticks.Where(a => a.Tick == t.Tick).Where(a => a.Win == win);
        //                if (query.Count() == 0)
        //                {
        //                    allticks.Add(t);
        //                }
        //                else
        //                {
        //                    query.First().Count += t.Count;
        //                }
        //            }
        //        }
        //        foreach (View.OperatorMonthTick tick in allticks)
        //        {
        //            Data.Month.GroupMonthWinOrLoseTick t = new GroupMonthWinOrLoseTick();
        //            t.GroupId = g.Id;
        //            t.Month = th.MPdate;
        //            t.Year = th.YPdate;
        //            t.Win = win;
        //            t.Tick = tick.Tick;
        //            t.Count = tick.Count;
        //            var query = db.GroupMonthWinOrLoseTick.Where(a => a.GroupId == g.Id).Where(a => a.Month == th.MPdate).Where(a => a.Year == th.YPdate).Where(a => a.Win == win).Where(a => a.Tick == tick.Tick);
        //            if (query.Count() > 0)
        //            {
        //                query.First().Count += tick.Count;
        //            }
        //            else
        //            {
        //                db.GroupMonthWinOrLoseTick.Add(t);
        //            }
        //        }
        //    }
        //    db.SaveChanges();
        //}

        //public List<GroupMonthWinOrLoseTick> GetGroupMonthWinOrLoseTick(int groupId, DB.DBManager db, int month, int year, bool win)
        //{
        //    var query = db.GroupMonthWinOrLoseTick.Where(a => a.GroupId == groupId).Where(a => a.Month == month).Where(a => a.Year == year).Where(a => a.Win == win);
        //    return query.ToList();
        //}

        //public List<GroupMonthWinOrLoseTick> GetSortedGroupMonthWinOrLoseTick(int groupId, DB.DBManager db, int month, int year, bool win)
        //{
        //    var query = db.GroupMonthWinOrLoseTick.Where(a => a.GroupId == groupId).Where(a => a.Month == month).Where(a => a.Year == year).Where(a => a.Win == win).OrderBy(a => a.Tick);

        //    List<GroupMonthWinOrLoseTick> ticks = new List<GroupMonthWinOrLoseTick>();
        //    ticks.Add(query.Where(a => a.Tick == 1).First());
        //    ticks.Add(query.Where(a => a.Tick == 2).First());
        //    ticks.Add(query.Where(a => a.Tick == 3).First());
        //    ticks.Add(query.Where(a => a.Tick == 4).First());
        //    ticks.Add(query.Where(a => a.Tick == 5).First());
        //    ticks.Add(query.Where(a => a.Tick == 6).First());
        //    ticks.Add(query.Where(a => a.Tick == 7).First());
        //    ticks.Add(query.Where(a => a.Tick == 8).First());
        //    ticks.Add(query.Where(a => a.Tick == 9).First());
        //    ticks.Add(query.Where(a => a.Tick == 10).First());
        //    var _query = query.Where(a => a.Tick > 10);
        //    GroupMonthWinOrLoseTick t = new GroupMonthWinOrLoseTick();
        //    t.Tick = 11;
        //    t.Count = _query.Sum(a => a.Count);
        //    ticks.Add(t);
        //    return ticks;
        //}



        //public bool HasSettlemented(int groupId, DB.DBManager db, int month, int year, bool win)
        //{
        //    var query = db.GroupMonthWinOrLoseTick.Where(a => a.GroupId == groupId).Where(a => a.Month == month).Where(a => a.Year == year).Where(a => a.Win == win).OrderBy(a => a.Tick);
        //    if (query.Count() > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}
    }
}